import wifiManager from '@ohos.wifiManager';
import data_preferences from '@ohos.data.preferences';


let wifiReadAble = true;


export function ipToHex(ip: string) {
  let arr = ip.split('.');
  let hex = '';
  for (let i = 0; i < arr.length; i++) {
    let num = parseInt(arr[i]); //大写字母
    hex += num.toString(16).toUpperCase();
  }
  return hex;
}

export function hexToIp(hex: string) {
  let arr = [];
  for (let i = 0; i < hex.length; i += 2) {
    arr.push(parseInt(hex.substr(i, 2).toLowerCase(), 16));
  }
  return arr.join('.');
}

export function ipToStr(ip: number): string {
  let strArr = [];
  for (let i = 0; i < 4; i++) {
    strArr.push((ip >> (i * 8)) & 0xff);
  }
  return strArr.reverse().join('.');
}

const defaultHex = "c0a80100".toUpperCase();


export function hexIpToDynamicLengthHexIP(hex: string) {

  let retStr = "";
  let i = 0;
  for (; i < hex.length; i += 1) {
    if (hex[i] != defaultHex[i]) break;
  }

  if (i === hex.length) {
    return "0";
  } else {
    for (; i < hex.length; i += 1) {
      retStr += hex[i];
    }
    return retStr;
  }
}

export function dynamicLengthHexIPToHexIP(dynamicLengthHexIP: string) {
  if (dynamicLengthHexIP === "0") {
    return defaultHex;
  } else {
    let len = dynamicLengthHexIP.length;
    let retStr = defaultHex.substr(0, 8 - len);
    retStr += dynamicLengthHexIP;
    return retStr;
  }
}

